<?php


// resize.php?t=c/

// resize.php?s=300|21&u=link

function getExtension($str) {     
	$i = strrpos($str,".");
	if (!$i) {
		return "";
	}
	$l = strlen($str) - $i;
	$ext = substr($str,$i+1,$l); 
	return $ext;
}
	# Avaible image
	
	if($_GET['u'] == "") $_GET['u'] = "/images/non-img.jpg";
	if( !isset ($_GET['u'])) {
		$_GET['u'] = "/images/non-img.jpg";
	}
	list($width_orig, $height_orig) = @getimagesize($_GET['u']);
	if ($width_orig < 10 || $height_orig < 10) $_GET['u'] = "/images/non-img.jpg";

	$type = $_GET['t'];
	if(!$type) $type =  'cut';
	
	
	$wh = explode('|',$_GET['s']);
	
	$width = $wh[0];
	$height =  $wh[1];
	$filename = $_GET['u'];
	
	list($width_orig, $height_orig, $imagetype, $attr) = getimagesize($filename);
	switch ($type) 
	{
		case 'cut' : 
		
			$image_p = imagecreatetruecolor($width, $height);
			$ratio_orig = $width_orig/$height_orig;
			if ($width/$height > $ratio_orig) {
			   $height = $width/$ratio_orig;
			} else {
				$width = $height*$ratio_orig;
			   
			}

			switch($imagetype)
			{
				case IMAGETYPE_JPEG :
					header('content-type: image/jpeg'); 
					$image = imagecreatefromjpeg($filename);
					imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
					imagejpeg($image_p, null, 100);
										
				break;
				case IMAGETYPE_PNG :
					header('content-type: image/png'); 
					$image = imagecreatefrompng($filename);
					imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
					imagepng($image_p, null, 100);
				break;
				case IMAGETYPE_GIF :
					header('content-type: image/gif'); 
					$image = imagecreatefromgif($filename);
					imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
					imagegif($image_p, null, 100);
				break;				
			}

	break ;
	case 'ratio' : 
			
			if ($height > $height_orig)  {
				$height = $height_orig ;
			}
			
			$ratio_orig = $width_orig/$height_orig;
			
			if ($width/$height > $ratio_orig) {
				$width = $height*$ratio_orig;			   
			} else {
				$height = $width/$ratio_orig;
	   
			}
			$image_p = imagecreatetruecolor($width, $height);
		
			switch($imagetype) {
				case IMAGETYPE_JPEG :
					header('content-type: image/jpeg'); 
					$image = imagecreatefromjpeg($filename);
					imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
					imagejpeg($image_p, null, 100);
				break;
				case IMAGETYPE_PNG :
					header('content-type: image/png'); 
					$image = imagecreatefrompng($filename);
					imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
					imagepng($image_p, null, 100);
				break;
				case IMAGETYPE_GIF :
					header('content-type: image/gif'); 
					$image = imagecreatefromgif($filename);
					imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
					imagegif($image_p, null, 100);
				break;				
			}
	break ;
	case 'center' : 
			/*if ($height > $height_orig)  {
				$height = $height_orig ;
			}
			
			$ratio_orig = $width_orig/$height_orig;
			
			if ($width/$height > $ratio_orig) {
				$width = $height*$ratio_orig;			   
			} else {
				$height = $width/$ratio_orig;
	   
			}*/
			
		
			
			$width_res = 200;
			$height_res = 300;
			
			$ratio_orig = $width_orig/$height_orig;
			$ratio_fram = $width/$height;
			
			// Anh vuong
			
			// Neu chieu rong lon hon chieu dai , thi chieu rong bang chieu rong , chieu cao bang chieu 
			
			if($ratio_fram == 1){
				if($ratio_orig > 1){
					$width_res = $width;
					$height_res = $width_res/$ratio_orig;
				}else {
					$height_res = $height;
					$width_res = $height_res*$ratio_orig;
				}
			}
			/*else if($ratio_fram > 1){
				// Rong hon cao , anh bet
				$height_res = $height;
				$width_res = $ratio_orig*$height_res;
			}else {
				// Anh cao
				$width_res = $width;
				$height_res = $width/$ratio_orig;
			}*/
			
			$image_p = imagecreatetruecolor($width, $height);
		
			imagefill($image_p,0,0,imagecolorallocate($image_p, 255, 255, 255));
			
			$xposition = abs(($width - $width_res))/2;
			$yposition = abs(($height - $height_res))/2;
		
			switch($imagetype) {
				case IMAGETYPE_JPEG :
					header('content-type: image/jpeg'); 
					$image = imagecreatefromjpeg($filename);
					imagecopyresampled($image_p, $image,$xposition ,$yposition,0,0, $width_res, $height_res, $width_orig, $height_orig);
					imagejpeg($image_p, null, 100);
				break;
				case IMAGETYPE_PNG :
					header('content-type: image/png'); 
					$image = imagecreatefrompng($filename);
					imagecopyresampled($image_p, $image,$xposition ,$yposition,0,0, $width_res, $height_res, $width_orig, $height_orig);
					imagepng($image_p, null, 100);
				break;
				case IMAGETYPE_GIF :
					header('content-type: image/gif'); 
					$image = imagecreatefromgif($filename);
					imagecopyresampled($image_p, $image,$xposition ,$yposition,0,0, $width_res, $height_res, $width_orig, $height_orig);
					imagegif($image_p, null, 100);
				break;				
			}
	break ;
	case 'center2' : 
			
			/*if ($height > $height_orig)  {
				$height = $height_orig ;
			}
			
			$ratio_orig = $width_orig/$height_orig;
			
			if ($width/$height > $ratio_orig) {
				$width = $height*$ratio_orig;			   
			} else {
				$height = $width/$ratio_orig;
	   
			}*/
			
			$width_res = 200;
			$height_res = 300;
			
			$ratio_orig = $width_orig/$height_orig;
			$ratio_fram = $width/$height;
			
			// Anh vuong
			
			// Neu chieu rong lon hon chieu dai , thi chieu rong bang chieu rong , chieu cao bang chieu 
			
			if($ratio_fram == 1){
				if($ratio_orig > 1){
					$width_res = $width;
					$height_res = $width_res/$ratio_orig;
				}else {
					$height_res = $height;
					$width_res = $height_res*$ratio_orig;
				}
			}
			else if($ratio_fram > 1){
				// Rong hon cao , anh bet
				$height_res = $height;
				$width_res = $ratio_orig*$height_res;
			}else {
				// Ket xuat cho ra anh cao . ##
				// Neu nhu anh vao bet hon anh ra thi lay chieu rong lam chuan 
				
				/*
				// Anh cao
				$width_res = $height*$ratio_orig;
				$height_res = $height;
				*/
				// Cao . cao
				if($ratio_orig >= 1){
					// Danh cho anh bet
					if(($ratio_orig - $ratio_fram) >0){
						//die("Anh bet ra anh cao");
						$width_res = $width;
						$height_res = $width_res/$ratio_orig;
					}
					else {
						/*
						die("Anh bet ra anh bet");						
	//					$height_res = $height;
	//					$width_res = $ratio_orig*$height_res;
						$width_res = $width;
						$height_res = $width_res/$ratio_orig;	
						*/
					}	
				}else {
					if(($ratio_orig - $ratio_fram) <0){
					$height_res = $height;
					$width_res = $ratio_orig*$height_res;
				}
				else {
//					$height_res = $height;
//					$width_res = $ratio_orig*$height_res;
					$width_res = $width;
					$height_res = $width_res/$ratio_orig;	
				}	
				}
			}
			
			$image_p = imagecreatetruecolor($width, $height);
			imagefill($image_p,0,0,imagecolorallocate($image_p, 255, 255, 255));
			
			$xposition = abs(($width - $width_res))/2;
			$yposition = abs(($height - $height_res))/2;
		
			switch($imagetype) {
				case IMAGETYPE_JPEG :
					header('content-type: image/jpeg'); 
					$image = imagecreatefromjpeg($filename);
					imagecopyresampled($image_p, $image,$xposition ,$yposition,0,0, $width_res, $height_res, $width_orig, $height_orig);
					imagejpeg($image_p, null, 100);
				break;
				case IMAGETYPE_PNG :
					header('content-type: image/png'); 
					$image = imagecreatefrompng($filename);
					imagecopyresampled($image_p, $image,$xposition ,$yposition,0,0, $width_res, $height_res, $width_orig, $height_orig);
					imagepng($image_p, null, 100);
				break;
				case IMAGETYPE_GIF :
					header('content-type: image/gif'); 
					$image = imagecreatefromgif($filename);
					imagecopyresampled($image_p, $image,$xposition ,$yposition,0,0, $width_res, $height_res, $width_orig, $height_orig);
					imagegif($image_p, null, 100);
				break;				
			}
	break ;
	}
	@imagedestroy($image);
	@imagedestroy($image_p);
?> 
